package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketIssue;
import com.cskaoyan.market.db.domain.MarketIssueExample;
import com.cskaoyan.market.db.mapper.MarketIssueMapper;
import com.cskaoyan.market.service.AdminIssueService;
import com.cskaoyan.market.util.MybatisUtils;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class AdminIssueServiceImpl implements AdminIssueService {
    @Override
    public List<MarketIssue> list(String id, String question, String answer, String addTime, String localDateTime, String page, String limit) {

        SqlSession session = MybatisUtils.getSession();
        MarketIssueMapper marketIssueMapper =  session.getMapper(MarketIssueMapper.class);
        MarketIssueExample marketIssueExample = new MarketIssueExample();
        MarketIssueExample.Criteria criteria = marketIssueExample.createCriteria();

        //根据输入的问题进行模糊查询
        if(!question.isEmpty()){
            criteria.andQuestionLike("%" + question + "%");
        }


        PageHelper.startPage(Integer.parseInt(page),Integer.parseInt(limit));

        List<MarketIssue> marketIssues = marketIssueMapper.selectByExample(marketIssueExample);
        session.close();
        return marketIssues;

    }

    @Override
    public int create(MarketIssue marketIssue) {

        SqlSession session = MybatisUtils.getSession();
        MarketIssueMapper marketIssueMapper =  session.getMapper(MarketIssueMapper.class);
        MarketIssueExample marketIssueExample = new MarketIssueExample();

        int effectedNumber =  marketIssueMapper.insertSelective(marketIssue);

        session.commit();
        session.close();
        return effectedNumber;

    }

    @Override
    public int update(MarketIssue marketIssue) {

        SqlSession session = MybatisUtils.getSession();
        MarketIssueMapper mapper = session.getMapper(MarketIssueMapper.class);
        int i = mapper.updateByPrimaryKeySelective(marketIssue);
        session.commit();
        session.close();
        return i;

    }

    @Override
    public int delete(MarketIssue marketIssue) {
        SqlSession session = MybatisUtils.getSession();

        int i = session.getMapper(MarketIssueMapper.class).deleteByPrimaryKey(marketIssue.getId());
        session.commit();
        session.close();
        return i;


    }



}
